package org.donald.g1.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author donald
 * 2018年3月15日
 * 上午9:19:40
 */
public class TaskThread extends Thread {
    private static final Logger log = LoggerFactory.getLogger(TaskThread.class);
    private final long creationTime;

    public TaskThread(ThreadGroup group, Runnable target, String name) {
        super(group, new WrapperRunnable(target), name);
        this.creationTime = System.currentTimeMillis();
    }

    public TaskThread(ThreadGroup group, Runnable target, String name,
            long stackSize) {
        super(group, new WrapperRunnable(target), name, stackSize);
        this.creationTime = System.currentTimeMillis();
    }

    /**
     * 
     * @return
     */
    public final long getCreationTime() {
        return creationTime;
    }

    private static class WrapperRunnable implements Runnable {
        private Runnable wrappedRunnable;
        WrapperRunnable(Runnable wrappedRunnable) {
            this.wrappedRunnable = wrappedRunnable;
        }
        @Override
        public void run() {
        	try{
        		wrappedRunnable.run();
        	}
            catch(Throwable t){
            	log.error("==={}异常：{}",Thread.currentThread().getName(),t.getMessage());
            	t.printStackTrace();
            }
        }
    }
}
